<!DOCTYPE html>
<html lang="zh-hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/wow/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/wow/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/wow/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/wow/images/logo.svg" color="#222">

<link rel="stylesheet" href="/wow/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"single-flower.gitee.io","root":"/wow/","images":"/wow/images","scheme":"Muse","version":"8.3.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"بحث...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="什么是RedisRedis是一个开源的内存数据库，它以键值对的形式存储数据。由于数据存储在内存中，因此Redis的速度很快，但是每次重启Redis服务时，其中的数据也会丢失，因此，Redis也提供了持久化存储机制(AOF,RDB)，将数据以某种形式保存在文件中，每次重启时，可以自动从文件加载数据到内存当中 什么是发布订阅Redis发布订阅（pub&#x2F;sub）是一种消息通信模式，发布者（publish">
<meta property="og:type" content="article">
<meta property="og:title" content="redis发布者订阅者">
<meta property="og:url" content="https://single-flower.gitee.io/wow/2021/02/10/zy/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/index.html">
<meta property="og:site_name" content="朵朵最可爱">
<meta property="og:description" content="什么是RedisRedis是一个开源的内存数据库，它以键值对的形式存储数据。由于数据存储在内存中，因此Redis的速度很快，但是每次重启Redis服务时，其中的数据也会丢失，因此，Redis也提供了持久化存储机制(AOF,RDB)，将数据以某种形式保存在文件中，每次重启时，可以自动从文件加载数据到内存当中 什么是发布订阅Redis发布订阅（pub&#x2F;sub）是一种消息通信模式，发布者（publish">
<meta property="og:locale">
<meta property="article:published_time" content="2021-02-10T08:49:50.631Z">
<meta property="article:modified_time" content="2021-04-13T07:05:42.398Z">
<meta property="article:author" content="X y">
<meta property="article:tag" content="-GIT">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://single-flower.gitee.io/wow/2021/02/10/zy/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-hans'
  };
</script>
<title>redis发布者订阅者 | 朵朵最可爱</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="تشغيل شريط التصفح" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/wow/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">朵朵最可爱</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/wow/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-about"><a href="/wow/about/" rel="section"><i class="fa fa-user fa-fw"></i>معلومات</a></li>
        <li class="menu-item menu-item-tags"><a href="/wow/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>الوسوم</a></li>
        <li class="menu-item menu-item-categories"><a href="/wow/categories/" rel="section"><i class="fa fa-th fa-fw"></i>التصنيفات</a></li>
        <li class="menu-item menu-item-archives"><a href="/wow/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>الأرشيفات</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          المحتويات
        </li>
        <li class="sidebar-nav-overview">
          عام
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AFRedis"><span class="nav-number">1.</span> <span class="nav-text">什么是Redis</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E5%8F%91%E5%B8%83%E8%AE%A2%E9%98%85"><span class="nav-number">2.</span> <span class="nav-text">什么是发布订阅</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%80%9A%E4%BF%97%E8%AF%B4%E7%9A%84%E8%AF%9D%EF%BC%8C%E5%B0%B1%E6%AF%94%E5%A6%82%E6%88%91sub%E7%AB%AF%EF%BC%88%E8%AE%A2%E9%98%85%E8%80%85%EF%BC%89%E4%B8%80%E7%9B%B4%E7%9B%91%E5%90%AC%E7%9D%80%EF%BC%8C%E4%B8%80%E6%97%A6pub%E7%AB%AF%EF%BC%88%E5%8F%91%E5%B8%83%E8%80%85%EF%BC%89%E5%8F%91%E5%B8%83%E4%BA%86%E6%B6%88%E6%81%AF%EF%BC%8C%E9%82%A3%E4%B9%88%E6%88%91%E5%B0%B1%E6%8E%A5%E6%94%B6%E8%BF%87%E6%9D%A5"><span class="nav-number">2.1.</span> <span class="nav-text">通俗说的话，就比如我sub端（订阅者）一直监听着，一旦pub端（发布者）发布了消息，那么我就接收过来</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Redis%E5%8F%91%E5%B8%83%E8%AE%A2%E9%98%85%E6%9E%B6%E6%9E%84"><span class="nav-number">3.</span> <span class="nav-text">Redis发布订阅架构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Redis%E5%8F%91%E5%B8%83%E8%AE%A2%E9%98%85%E6%A8%A1%E5%BC%8F%E4%BC%98%E7%82%B9"><span class="nav-number">4.</span> <span class="nav-text">Redis发布订阅模式优点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B8%B8%E8%A7%81%E5%91%BD%E4%BB%A4"><span class="nav-number">5.</span> <span class="nav-text">常见命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%BB%E7%BB%93%EF%BC%9A"><span class="nav-number">6.</span> <span class="nav-text">总结：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%8A%E5%A4%A9%E5%88%86%E4%BA%AB%E7%9A%84%E5%8D%9A%E5%AE%A2%E5%B0%B1%E5%88%B0%E8%BF%99%E9%87%8C%E5%92%AF-%E6%AC%A2%E8%BF%8E%E5%A4%A7%E5%AE%B6%E5%89%8D%E6%9D%A5%E7%95%99%E8%A8%80%E8%A7%82%E7%9C%8B%E5%93%A6"><span class="nav-number">6.1.</span> <span class="nav-text">今天分享的博客就到这里咯 欢迎大家前来留言观看哦</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">X y</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/wow/archives/">
        
          <span class="site-state-item-count">23</span>
          <span class="site-state-item-name">المقالات</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">التصنيفات</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">الوسوم</span>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-hans">
    <link itemprop="mainEntityOfPage" href="https://single-flower.gitee.io/wow/2021/02/10/zy/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wow/images/avatar.gif">
      <meta itemprop="name" content="X y">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="朵朵最可爱">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          redis发布者订阅者
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">نُشر في</span>

      <time title="أُنشأ: 2021-02-10 16:49:50" itemprop="dateCreated datePublished" datetime="2021-02-10T16:49:50+08:00">2021-02-10</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">عُدل في</span>
        <time title="عُدل: 2021-04-13 15:05:42" itemprop="dateModified" datetime="2021-04-13T15:05:42+08:00">2021-04-13</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">في</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/wow/categories/GIT/" itemprop="url" rel="index"><span itemprop="name">-GIT</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="什么是Redis"><a href="#什么是Redis" class="headerlink" title="什么是Redis"></a>什么是Redis</h2><p>Redis是一个开源的内存数据库，它以键值对的形式存储数据。由于数据存储在内存中，因此Redis的速度很快，但是每次重启Redis服务时，其中的数据也会丢失，因此，Redis也提供了持久化存储机制(AOF,RDB)，将数据以某种形式保存在文件中，每次重启时，可以自动从文件加载数据到内存当中</p>
<h2 id="什么是发布订阅"><a href="#什么是发布订阅" class="headerlink" title="什么是发布订阅"></a>什么是发布订阅</h2><p>Redis发布订阅（pub/sub）是一种消息通信模式，发布者（publish）发送消息，订阅者（subscribe）接收消息<br>订阅者（如客户端）以事件订阅的方式表达出它有兴趣接收的一个事件或一类事件<br>发布者（如服务器）可将订阅者感兴趣的事件随时通知相关订阅者</p>
<h3 id="通俗说的话，就比如我sub端（订阅者）一直监听着，一旦pub端（发布者）发布了消息，那么我就接收过来"><a href="#通俗说的话，就比如我sub端（订阅者）一直监听着，一旦pub端（发布者）发布了消息，那么我就接收过来" class="headerlink" title="通俗说的话，就比如我sub端（订阅者）一直监听着，一旦pub端（发布者）发布了消息，那么我就接收过来"></a>通俗说的话，就比如我sub端（订阅者）一直监听着，一旦pub端（发布者）发布了消息，那么我就接收过来</h3><h2 id="Redis发布订阅架构"><a href="#Redis发布订阅架构" class="headerlink" title="Redis发布订阅架构"></a>Redis发布订阅架构</h2><p>Redis提供了发布订阅功能，可以用于消息的传输，Redis的发布订阅机制包括三个部分：发布者，订阅者，Channel</p>
<p>发布者和订阅者都是Redis客户端，Channel则为Redis服务器端，发布者将消息发送到某个的频道，订阅了这个频道的订阅者就能接收到这条消息。<br>Redis的这种发布订阅机制与基于主题的发布订阅类似，Channel相当于主题</p>
<h2 id="Redis发布订阅模式优点"><a href="#Redis发布订阅模式优点" class="headerlink" title="Redis发布订阅模式优点"></a>Redis发布订阅模式优点</h2><p>发送者（发布者）不是计划发送消息给特定的接收者（订阅者）。<br>而是发布的消息分到不同的频道，不需要知道什么样的订阅者订阅。<br>订阅者对一个或多个频道感兴趣，只需接收感兴趣的消息，不需要知道什么样的发布者发布的。<br>这种发布者和订阅者的解耦合可以带来更大的扩展性和更加动态的网络拓扑</p>
<h2 id="常见命令"><a href="#常见命令" class="headerlink" title="常见命令"></a>常见命令</h2><p>Psubscribe</p>
<p>Redis Psubscribe 命令订阅一个或多个符合给定模式的频道<br>Pubsub</p>
<p>Redis Pubsub 命令用于查看订阅与发布系统状态，它由数个不同格式的子命令组成<br>Publish</p>
<p>Redis Publish 命令用于将信息发送到指定的频道<br>Punsubscribe</p>
<p>Redis Punsubscribe 命令用于退订所有给定模式的频道<br>Subscribe</p>
<p>Redis Subscribe 命令用于订阅给定的一个或多个频道的信息<br>Unsubscribe</p>
<p>Redis Unsubscribe 命令用于退订给定的一个或多个频道的信息</p>
<h2 id="总结："><a href="#总结：" class="headerlink" title="总结："></a>总结：</h2><p>发布订阅是消息队列的一种方式，基于消息队列的方式，可以实现系统解耦、削峰填谷，顶住流量洪峰<br>redis的主业目前是基于键值对的数据存储、缓存等，消息队列可能是redis的一种尝试，但对redis消息队列以后的发展我们可以保持持续关注</p>
<h3 id="今天分享的博客就到这里咯-欢迎大家前来留言观看哦"><a href="#今天分享的博客就到这里咯-欢迎大家前来留言观看哦" class="headerlink" title="今天分享的博客就到这里咯 欢迎大家前来留言观看哦"></a>今天分享的博客就到这里咯 欢迎大家前来留言观看哦</h3>
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/wow/tags/GIT/" rel="tag"># -GIT</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/wow/2021/02/10/zy/axios%E5%B0%81%E8%A3%85/" rel="prev" title="axios封装">
                  <i class="fa fa-chevron-left"></i> axios封装
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/wow/2021/02/11/zy/webshell/" rel="next" title="webshell">
                  webshell <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">X y</span>
</div>
  <div class="powered-by">تطبيق الموقع <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/wow/js/utils.js"></script><script src="/wow/js/motion.js"></script><script src="/wow/js/schemes/muse.js"></script><script src="/wow/js/next-boot.js"></script>

  






  





</body>
</html>
